home *** CD-ROM | disk | FTP | other *** search
/ Amiga Collections: Taifun / Taifun 072 (1988-11-15)(Ossowski, Stefan)(DE)(PD).zip / Taifun 072 (1988-11-15)(Ossowski, Stefan)(DE)(PD).adf / Surveyor / surveyor.c < prev    next >
C/C++ Source or Header  |  1988-08-14  |  7KB  |  256 lines

  1. /*  char *Cr="Surveyor 1.0  13 April 1988 by Dirk Reisig\n";  */
  2.  
  3. /*  Surveyor displays the pointer position relative to the screens
  4.  *  upperleftmost and to a free postionable point and it shows the
  5.  *  color the pointer is on. It does it on all intuition screens
  6.  *  that are frontmost. (and somtimes trashing them).
  7.  *  
  8.  *  This program may be freely distributed.
  9.  */
  10.  
  11. #include "exec/types.h"
  12. #include "intuition/intuition.h"
  13. #include "intuition/intuitionbase.h"
  14. #include "graphics/rastport.h"
  15.  
  16. #define HWEXTRA        8        
  17. #define VWEXTRA        4        
  18. #define MOUSESPEED    4        
  19. #define IB        IntuitionBase    
  20.  
  21. /* I tried a higher value (8) for the mouse speed. It resulted in a
  22.  * very strange behavior. The pointer was attracted by the
  23.  * lower right corner. A interference problem?
  24.  */
  25.  
  26. typedef struct mousepointerdescriptor{
  27.   USHORT *Pointer;
  28.   int Height, Width, XOffset, YOffset;
  29. } MousePtr;
  30.  
  31. extern void PutWindowAway(), ChangePrefs(), ChangePointer();
  32. extern char *RAdCi_d(); 
  33. extern struct Window *OpenWindow();
  34. extern short EmptyData[], CrossData[];
  35. extern struct IntuiMessage *GetMsg();
  36.  
  37. struct IB *IB;
  38. struct Window *W;
  39. MousePtr Empty={ EmptyData, 0, 0, 0, 0 };
  40. MousePtr Cross={ CrossData,15,15,-8,-7 };
  41.  
  42. char  TxAbs[]="A          ";
  43. char  TxRel[]="R          ";
  44. char  TxCol[]="Color      ";
  45. char TxWTtl[]="Q    X    Y";
  46.  
  47. struct IntuiText  ITAbs={ 1,0,JAM2,HWEXTRA/2,0,0,TxAbs,     0 };
  48. struct IntuiText  ITRel={ 1,0,JAM2,HWEXTRA/2,0,0,TxRel,&ITAbs };
  49. struct IntuiText  ITCol={ 1,0,JAM2,HWEXTRA/2,0,0,TxCol,&ITRel };
  50. struct IntuiText ITWTtl={ 1,0,JAM2,0,0,0,TxWTtl,0 };
  51.  
  52. struct Gadget GClose={ 
  53.   0,0,0,0,10,GADGHCOMP,TOGGLESELECT,BOOLGADGET,0,0,0,0,0,0,0
  54. };
  55.  
  56. struct NewWindow NW={
  57.   0,0,0,0,-1,-1,
  58.   MOUSEBUTTONS,
  59.   WINDOWDRAG | NOCAREREFRESH | RMBTRAP ,
  60.   0,0,TxWTtl,0,0,0,0,0,0,0
  61. };
  62.  
  63.  
  64. _main()
  65. {
  66.   register short NowX, NowY, DynaCol, StatX, StatY, DynaX, DynaY;
  67.   short StatCol, MouseButton;
  68.   short PrintX=-1, PrintY=-1, PrintCol=-1, PrintStat=-1;
  69.   short Cycle=0, DisplayDots=0, WP=0;
  70.   register struct Screen *FS=0;
  71.   register struct IntuiMessage *Mesg; /* No register as promised */
  72.   
  73.   if (!(IB=(struct IB *)OpenLibrary("intuition.library",0))) return(0);
  74.   while (1){
  75.     if (FS!=IB->FirstScreen){
  76.       Forbid();
  77.       FS=IB->FirstScreen;
  78.       if (!GetNewWindow(FS)) break;
  79.       Permit();
  80.       StatX=0;  StatY=0;
  81.       DisplayDots=0;
  82.       ChangePrefs(0);
  83.     }
  84.     if (IB->FirstScreen->LayerInfo.top_layer!=W->WLayer){
  85.       Forbid();   /* Our window is not firstmost */
  86.       if (FS==IB->FirstScreen)  WindowToFront(W);
  87.       Permit();
  88.     }
  89.     NowX=FS->MouseX;  NowY=FS->MouseY;
  90.     MouseButton=0;    
  91.     if (Mesg=GetMsg(W->UserPort)){   
  92.       if (Mesg->Class==MOUSEBUTTONS) MouseButton=Mesg->Code;
  93.       ReplyMsg(Mesg);
  94.     }
  95.     if (MouseButton==MENUDOWN){  /* From spritepointer to dot? */
  96.       DynaCol=GetDotColor(NowX,NowY);
  97.       StatCol=GetDotColor(StatX,StatY);
  98.       DynaX=NowX;  DynaY=NowY;
  99.       ChangePrefs(1);  
  100.       DisplayDots=1;   
  101.     }
  102.     if (DisplayDots){
  103.       if (MouseButton==SELECTDOWN){ /* Replace relative point? */
  104.         SetDotColor(StatX,StatY,StatCol);
  105.         StatX=NowX;  StatY=NowY;
  106.         StatCol=DynaCol;
  107.         PrintStat=1;
  108.       }
  109.       if ((NowX!=DynaX)||(NowY!=DynaY)){  /* Pointer moved? */
  110.         SetDotColor(DynaX,DynaY,DynaCol);
  111.         DynaX=NowX;  DynaY=NowY;
  112.         DynaCol=GetDotColor(DynaX,DynaY);
  113.       }
  114.       /* (++Cycle)%=(1<<FS->BitMap.Depth); should be, but is inherent */
  115.       ++Cycle;
  116.       SetDotColor(DynaX,DynaY,Cycle);
  117.       SetDotColor(StatX,StatY,Cycle);
  118.       if (MouseButton==MENUUP){          /* Back to spritepointer? */
  119.         SetDotColor(DynaX,DynaY,DynaCol);
  120.         SetDotColor(StatX,StatY,StatCol);
  121.         ChangePrefs(0);
  122.         DisplayDots=0;
  123.       }
  124.     } else{ /* !DisplayDots */
  125.       DynaCol=GetDotColor(NowX,NowY); /* Did color change under pointer? */
  126.       if ((W->Flags&WINDOWACTIVE)&&!WP){
  127.         ChangePointer(&Cross);
  128.         WP=1;
  129.       } else WP=0;
  130.       if (GClose.Flags&SELECTED) break;
  131.     } /* DisplayDots */
  132.     if ((NowX!=PrintX)||(NowY!=PrintY)||(DynaCol!=PrintCol)||PrintStat){
  133.       PrintX=NowX;  PrintY=NowY;  PrintCol=DynaCol;  PrintStat=0;
  134.       strcpy(&TxCol[7],RAdCi_d(4,DynaCol));
  135.       strcpy(&TxRel[2],RAdCi_d(4,NowX-StatX));
  136.       strcpy(&TxRel[6],RAdCi_d(5,NowY-StatY));
  137.       strcpy(&TxAbs[2],RAdCi_d(4,NowX));
  138.       strcpy(&TxAbs[6],RAdCi_d(5,NowY));
  139.       Forbid();
  140.       if (FS==IB->FirstScreen)  PrintIText(W->RPort,&ITCol,0,0);
  141.       Permit();
  142.     }
  143.     /*  Most of the time, this program blocks intuition. So give the
  144.      *  system some more time by delaying this loop. That even increases
  145.      *  this programs performance. Isn't that peculiar?
  146.      */
  147.     Delay(1);
  148.   }
  149.   ChangePrefs(0);
  150.   if (W)  PutWindowAway(W);
  151.   CloseLibrary(IB);
  152.   return(0);
  153. }
  154.  
  155.  
  156. GetNewWindow(FS)
  157. register struct Screen *FS;
  158. {
  159.   static short  VFact=0, HFact=0;
  160.   register short TxLen_d;
  161.  
  162.   if (W){
  163.     NW.LeftEdge=W->LeftEdge<<HFact; /* scale from */
  164.     NW.TopEdge=W->TopEdge<<VFact;
  165.     PutWindowAway(W);
  166.   } 
  167.   ITAbs.TopEdge=1*FS->Font->ta_YSize+((VWEXTRA*3)/4);  /* set text pos's */
  168.   ITRel.TopEdge=2*FS->Font->ta_YSize+((VWEXTRA*3)/4);
  169.   ITCol.TopEdge=3*FS->Font->ta_YSize+((VWEXTRA*3)/4);
  170.   ITWTtl.ITextFont=FS->Font;
  171.   TxLen_d=IntuiTextLength(&ITWTtl);
  172.   GClose.Width=TxLen_d/11+(HWEXTRA/2);
  173.   NW.LeftEdge>>=HFact=(FS->ViewPort.Modes&HIRES)?0:1;  /* scale to */
  174.   NW.TopEdge>>= VFact=(FS->ViewPort.Modes&LACE)?0:1;
  175.   NW.Width=TxLen_d+HWEXTRA;
  176.   NW.Height=4*FS->Font->ta_YSize+VWEXTRA;
  177.   NW.Screen=FS;
  178.   NW.Type=FS->Flags&SCREENTYPE;
  179.   while ((NW.LeftEdge+NW.Width)>FS->Width) --NW.LeftEdge;  /* inside screen? */
  180.   while ((NW.TopEdge+NW.Height)>FS->Height) --NW.TopEdge;
  181.   if (W=(struct Window *)OpenWindow(&NW))  AddGadget(W,&GClose,0);  
  182.   return(W);
  183. }
  184.  
  185.  
  186. /*
  187.  *  We donot want to close a window on a non existing screen, do we?
  188.  */
  189.  
  190. void PutWindowAway(window)
  191. register struct Window *window;
  192. {
  193. extern struct IntuitionBase *IntuitionBase;
  194.   register struct Screen *S;
  195.  
  196.   for (S=IntuitionBase->FirstScreen;S;S=S->NextScreen){
  197.     if (S==window->WScreen){
  198.       CloseWindow(window);
  199.       break;
  200.     }
  201.   }
  202. }
  203.  
  204.  
  205. /*
  206.  *  Here we change the pointer speed. And the pointer.
  207.  */
  208.  
  209. void ChangePrefs(yn)
  210. short yn;
  211. {
  212.   static short ItsSpeed;
  213.   struct Preferences Prefs;
  214.   Forbid();
  215.   if (yn){
  216.     ChangePointer(&Empty);
  217.     GetPrefs(&Prefs,sizeof(struct Preferences));
  218.     ItsSpeed=Prefs.PointerTicks;
  219.     Prefs.PointerTicks=MOUSESPEED;
  220.     SetPrefs(&Prefs,sizeof(struct Preferences),FALSE);
  221.   } else{
  222.     ChangePointer(&Cross);
  223.     GetPrefs(&Prefs,sizeof(struct Preferences));
  224.     if (Prefs.PointerTicks==MOUSESPEED){
  225.       Prefs.PointerTicks=ItsSpeed;
  226.       SetPrefs(&Prefs,sizeof(struct Preferences),FALSE);
  227.     } else  ItsSpeed=Prefs.PointerTicks;
  228.   }
  229.   Permit();
  230. }    
  231.  
  232.  
  233. /*
  234.  * Some simple formatting routine ( return ("%<size>d",number)
  235.  */
  236. char *RAdCi_d(size,number)
  237. {
  238.   static char buffer[32];
  239.   register  short i;
  240.   for (i=0;i<32;++i) buffer[i]=' ';
  241.   return(&buffer[15-size+stci_d(&buffer[15],number)]);
  242. }
  243.  
  244.  
  245. void MemCleanup(){}  /* This really cleans up a lot! */
  246.  
  247. /*
  248.  *  Some trivial function, but saving code. It is possible to pass a
  249.  *  structure. Why not use it once? It shurely will produce a warning!
  250.  */
  251. void ChangePointer(Ptr)
  252. MousePtr *Ptr;
  253. {
  254.   SetPointer(W,*Ptr);
  255. }
  256.